package com.cms.pms.impl;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import com.cms.pms.Aggregator;
import com.cms.pms.Configuration;
import com.cms.pms.PMSContext;
import com.cms.pms.Parser;
import com.cms.pms.Processor;
import com.cms.pms.aggregator.SimpleConverter;


public class DefaultConfiguration implements Configuration {

	DefaultContext context = new DefaultContext();
	DefaultAggregator aggregator = new DefaultAggregator();
	DefaultParser parser = new DefaultParser();

	ProcessorImpl processor = new ProcessorImpl();

	ApplicationContext applicationContext;

	public void setInterval(int interval) {
		this.processor.setInterval(interval);
	}

	@Override
	public void build() {

		PMSContext.setConfiguration(this);

		PMSContext.setContext(context);
		PMSContext.setAggregator(aggregator);
		PMSContext.setParser(parser);
		
		PMSContext.setProcessor(processor);

		Map<String, Aggregator> aggregators = this.applicationContext.getBeansOfType(Aggregator.class);
		for (Aggregator ag : aggregators.values()) {
			this.aggregator.addAggregator(ag);
		}

		Map<String, Parser> parsers = this.applicationContext.getBeansOfType(Parser.class);
		for (Parser p : parsers.values()) {
			this.parser.addParser(p);
		}
		
		processor.start();
	}

	public int getInterval() {
		return this.processor.getInterval();
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		this.applicationContext = arg0;
	}

	@Override
	public ApplicationContext getApplicatonContext() {
		return this.applicationContext;
	}
}
